package com.bw.test;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class IOUtils {
    public static void main(String[] args) {

    }
    //关闭流
    public static void closeStream(Closeable... closeables){
        for (Closeable closeable : closeables) {
            try {
                closeable.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    //拷贝文件
    public  static void copyFile(String oldfile,String targetfile){
        try {
            FileInputStream input = new FileInputStream(new File(oldfile));
            FileOutputStream output = new FileOutputStream(new File(targetfile));
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
    //根据流copy文件
    public static void copyStream(InputStream input,OutputStream output){
        BufferedInputStream bis=null;
        BufferedOutputStream bos=null;

        try {
            if(input==null || output == null ) return;
                //输入流缓冲区
                bis = new BufferedInputStream(input);
                //输出流缓冲区
                bos = new BufferedOutputStream(output);
            byte[] buffer = new byte[1024];
            while (bis.read(buffer)!=-1){
                bos.write(buffer);
            }
            bos.flush();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeStream(input,output,bis,bos);
        }
    }
    //按行读取文本文件返回一个list集合；根据file
    public static List<String> redaTextFile(File file){
        FileReader fileReader=null;
        BufferedReader br=null;
        try {
            if(file==null){
                return null;
            }
            //创建集合用来收数据
            ArrayList<String> list = new ArrayList<>();
            //输入字符流
             fileReader = new FileReader(file);
             //输入流缓冲区
            br=new BufferedReader(fileReader);
            String readLine=null;
            while ((readLine = br.readLine())!=null){
                list.add(readLine);
            }
            return list;
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
        }
        return null;
    }
}
